package liulin.com.discount.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import liulin.com.discount.R;
import liulin.com.discount.adapter.ViewPagerAdapter;
import liulin.com.discount.fragment.FragmentCollection;
import liulin.com.discount.fragment.Fragment_discover;
import liulin.com.discount.fragment.FragmentDiscount;
import liulin.com.discount.fragment.Fragment_mine;

public class MainActivity extends FragmentActivity {
    @BindView(R.id.rb_findDiscount)
    RadioButton rbFindDiscount;
    @BindView(R.id.rb_collect)
    RadioButton rbCollect;
    @BindView(R.id.rb_discover)
    RadioButton rbDiscover;
    @BindView(R.id.rb_mine)
    RadioButton rbMine;

    /**
     * 作为页面容器的ViewPager
     */
    @BindView(R.id.viewpager)
    ViewPager myViewPager;
    @BindView(R.id.rg_main_tab)
    RadioGroup mradioGroup;

    //页面集合
    private  ArrayList<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiy_main);
        ButterKnife.bind(this);
        //RadioGroup选中状态改变监听
        mradioGroup.setOnCheckedChangeListener(new myCheckChangeListener());
        initViewPager();
    }

    private void initViewPager(){
        //四个页面
        FragmentDiscount fragmentFinddiscount=new FragmentDiscount();
        FragmentCollection fragmentCollect=new FragmentCollection();
        Fragment_discover fragmentDiscover=new Fragment_discover();
        Fragment_mine fragmentMime=new Fragment_mine();

        fragmentList=new ArrayList<Fragment>();
        fragmentList.add(fragmentFinddiscount);
        fragmentList.add(fragmentCollect);
        fragmentList.add(fragmentDiscover);
        fragmentList.add(fragmentMime);

        //ViewPager设置适配器
        myViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), fragmentList));
        //ViewPager显示第一个Fragment
        myViewPager.setCurrentItem(0);
        //ViewPager页面切换监听
        myViewPager.addOnPageChangeListener(new myOnPageChangeListener());
    }

    /**
     *RadioButton切换Fragment
     */
    private class myCheckChangeListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb_findDiscount:
                    //ViewPager显示第一个Fragment且关闭页面切换动画效果
                    myViewPager.setCurrentItem(0,false);
                    break;
                case R.id.rb_collect:
                    myViewPager.setCurrentItem(1,false);
                    break;
                case R.id.rb_discover:
                    myViewPager.setCurrentItem(2,false);
                    break;
                case R.id.rb_mine:
                    myViewPager.setCurrentItem(3,false);
                    break;
            }
        }
    }

    /**
     *ViewPager切换Fragment,RadioGroup做相应变化
     */
    private class myOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position){
                case 0:
                    mradioGroup.check(R.id.rb_findDiscount);
                    break;
                case 1:
                    mradioGroup.check(R.id.rb_discover);
                    break;
                case 2:
                    mradioGroup.check(R.id.rb_collect);
                    break;
                case 3:
                    mradioGroup.check(R.id.rb_mine);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}